package com.tomkk;

import com.google.gson.Gson;
import com.tomkk.entity.Achievement;
import com.tomkk.entity.AchievementComment;
import com.tomkk.utils.Error;
import com.tomkk.utils.MysqlOperation;
import com.tomkk.utils.Success;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Created by Tomkk on 2016/5/28.
 */
@WebServlet(name = "GetAchievementServlet",urlPatterns = "/getAchievement")
public class GetAchievementServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(request.getParameter("achievement_id"));
        String sql = "SELECT achievement_title,achievement_content,achievement_id,nickname,achievement_isuse,achievement_publish_time FROM achievement,user WHERE achievement_id = "+id+" AND achievement.user_id = user.user_id;";
        MysqlOperation operation = new MysqlOperation();
        operation.checkConn();
        ResultSet resultSet = operation.query(sql);
        Gson gson = new Gson();
        String json = null;
        if(resultSet == null){
            json = gson.toJson(new Error(2,"服务器错误"));
        }else{
            try {
                resultSet.next();
                String achievement_title = resultSet.getString("achievement_title");
                String achievement_content = resultSet.getString("achievement_content");
                int achievement_id = resultSet.getInt("achievement_id");
                String nickname = resultSet.getString("nickname");
                boolean achievement_isuse = resultSet.getBoolean("achievement_isuse");
                String achievement_publish_time = resultSet.getString("achievement_publish_time");

                sql = "SELECT user.nickname as comment_publisher_nickname,comment_content,achievement_comment.publish_time,comment_id,headimage,user.user_id FROM achievement_comment NATURAL LEFT JOIN user WHERE comment_isuse = true AND achievement_comment.achievement_id = "+achievement_id+";";

                resultSet = operation.query(sql);
                String comment_publisher_nickname = null;
                String comment_content = null;
                String comment_publish_time = null;
                int comment_id;
                int user_id;//发表评论者id
                String headimage = null;
                ArrayList<AchievementComment> comments = new ArrayList<>();
                while(resultSet.next()){
                    comment_publisher_nickname = resultSet.getString("comment_publisher_nickname");
                    comment_content = resultSet.getString("comment_content");
                    comment_publish_time = resultSet.getString("publish_time");
                    comment_id = resultSet.getInt("comment_id");
                    headimage = resultSet.getString("headimage");
                    user_id = resultSet.getInt("user_id");
                    AchievementComment comment = new AchievementComment(comment_publisher_nickname,comment_content,comment_publish_time,comment_id,headimage,user_id);
                    comments.add(comment);
                }
                Achievement achievement = new Achievement(achievement_title,achievement_content,achievement_id,nickname,achievement_isuse,achievement_publish_time,comments);
                json = gson.toJson(new Success(0,achievement));
                operation.closeDB();
            } catch (SQLException e) {
                e.printStackTrace();
                json = gson.toJson(new Error(2,"服务器错误"));
            }
        }
        PrintWriter writer = response.getWriter();
        writer.print(json);
        writer.flush();

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
